package com.example.opengldemo.jbulletJava;

import android.opengl.GLES30;
import android.view.View;

import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.TriangleIndexVertexArray;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.constraintsolver.Point2PointConstraint;
import com.bulletphysics.extras.gimpact.GImpactMeshShape;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.MatrixUtils;
import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.vecmath.Vector3f;

public class ObjectVertexTexcoorsNormal {

    public final CollisionShape loadShape;
    public SimpleShaderHandle shaderHandle;
    public SimpleShaderBuffer shaderBuffer;


    float[] color=new float[]{1,1,1,1};

    View view ;
    float[] vertexArray ;
    float[] normalArray ;
    float[] texcoorsArray ;
    public ObjectVertexTexcoorsNormal(View view , float[] vertexArray , float[] normalArray , float[] texcoorsArray ){
        this.view = view ;
        this.vertexArray = vertexArray ;
        this.normalArray = normalArray ;
        this.texcoorsArray = texcoorsArray ;
        
        shaderBuffer = new SimpleShaderBuffer();
        shaderBuffer.setVertexBuffer(vertexArray);
        shaderBuffer.setNormalBuffer(normalArray);
        shaderBuffer.setTexcoorBuffer(texcoorsArray);
        float colors[]=new float[shaderBuffer.vCount*4];
        for(int i=0;i<shaderBuffer.vCount;i++)
        {
            colors[i*4]=1;
            colors[i*4+1]=0;
            colors[i*4+2]=0;
            colors[i*4+3]=1;
        };
        shaderBuffer.setColorBuffer(colors);
        for(int i=0;i<shaderBuffer.vCount;i++)
        {
            colors[i*4]=color[0];
            colors[i*4+1]=color[1];
            colors[i*4+2]=color[2];
            colors[i*4+3]=color[3];
        };
        shaderBuffer.setColorBuffer2(colors);

        //开辟缓存空间大小
        ByteBuffer gVertices=ByteBuffer.allocateDirect(vertexArray.length*4).order(ByteOrder.nativeOrder());
        for(int i=0;i<vertexArray.length;i++)
        {
            gVertices.putFloat(i*4,vertexArray[i]);
        }
        gVertices.position(0);

        ByteBuffer gIndices=ByteBuffer.allocateDirect(shaderBuffer.vCount*4).order(ByteOrder.nativeOrder());
        for(int i=0;i<shaderBuffer.vCount;i++)
        {
            gIndices.putInt(i);
        }
        gIndices.position(0);

        int vertStride = 4*3;
        int indexStride = 4*3;
        TriangleIndexVertexArray indexVertexArrays= new TriangleIndexVertexArray(
                                shaderBuffer.vCount/3,
                                gIndices,
                                indexStride,
                                shaderBuffer.vCount,
                                gVertices,
                                vertStride
                        );

        GImpactMeshShape trimesh = new GImpactMeshShape(indexVertexArrays);
        trimesh.updateBound();
        loadShape =trimesh;
    }

    public ObjectVertexTexcoorsNormal clone(){
        return new ObjectVertexTexcoorsNormal(view,vertexArray,normalArray  , texcoorsArray);
    }

    public void initShader(int program){
        shaderHandle = new SimpleShaderHandle(program);
    }

    public void drawSelf(int textureid ) {
        shaderHandle.program()
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindNormalHandle(shaderBuffer.getNormalBuffer())
                .bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer())
                .bindMVPMaxtrixHandle()
                .bindMMaxtrixHandle()
                .bindLightHandle(MatrixState.lightLocationFB)
                .bindCameraHandle(MatrixState.cameraLocationFB)
                .bindDefaultTextureHandle(textureid)
                .run(GLES30.GL_TRIANGLES,shaderBuffer.vCount);
    }
}
